package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.impl.SysArtistExtServiceImpl;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.web.apimodel.system.artistlog.ArtistExtAdd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysArtistExt;
import com.ruoyi.system.service.ISysArtistExtService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 艺人资料关联Controller
 * 
 * @author Yaphets
 * @date 2024-07-07
 */

@Api(tags = "艺人附件资料")
@RestController
@RequestMapping("/system/artistExt")
public class SysArtistExtController extends BaseController
{
    @Autowired
    private SysArtistExtServiceImpl sysArtistExtService;

    /**
     * 查询艺人资料关联列表
     */
//    @PreAuthorize("@ss.hasPermi('system:artistExt:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysArtistExt sysArtistExt)
    {
        startPage();
        List<SysArtistExt> list = sysArtistExtService.selectSysArtistExtList(sysArtistExt);
        return getDataTable(list);
    }

    /**
     * 导出艺人资料关联列表
     */
//    @PreAuthorize("@ss.hasPermi('system:artistExt:export')")
    @Log(title = "艺人资料关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysArtistExt sysArtistExt)
    {
        List<SysArtistExt> list = sysArtistExtService.selectSysArtistExtList(sysArtistExt);
        ExcelUtil<SysArtistExt> util = new ExcelUtil<SysArtistExt>(SysArtistExt.class);
        util.exportExcel(response, list, "艺人资料关联数据");
    }

    /**
     * 获取艺人资料关联详细信息
     */
    @ApiOperation(value = "获取艺人资料信息")
    @ApiResponse(code = 200, message = "成功", response = SysArtistExt.class)
//    @PreAuthorize("@ss.hasPermi('system:artistExt:query')")
    @PostMapping("/getInfo")
    public AjaxResult getInfo(@RequestBody SysArtistExt sysArtistExt)
    {

        return success(sysArtistExtService.selectSysArtistExtById(sysArtistExt.getProjectId(),sysArtistExt.getArtistId()));
    }

    /**
     * 新增艺人资料关联
     */

    @ApiOperation(value = "新增艺人资料信息")
    @ApiResponse(code = 200, message = "成功",response = SysArtistExt.class)
//    @PreAuthorize("@ss.hasPermi('system:artistExt:add')")
    @Log(title = "艺人资料关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArtistExtAdd artistExtAdd)
    {
        List<ArtistExtAdd.info> videos = artistExtAdd.getVideoList();
        List<ArtistExtAdd.info> attrs = artistExtAdd.getAttrList();
        //先删
        sysArtistExtService.deleteSysArtistExtByTwoId(artistExtAdd.getProjectId(),artistExtAdd.getArtistId());
        //插入新视频信息
        if(videos != null && !videos.isEmpty()){
            for (ArtistExtAdd.info video : videos){
                sysArtistExtService.insertSysArtistExt(new SysArtistExt(artistExtAdd.getProjectId(),artistExtAdd.getArtistId(),artistExtAdd.getArtistName(),0,video.getName(),video.getUrl()));
            }
//            videos.stream().map( video -> sysArtistExtService.insertSysArtistExt(new SysArtistExt(artistExtAdd.getProjectId(),artistExtAdd.getArtistId(),artistExtAdd.getArtistName(),0,video.getName(),video.getUrl())));
        }
        //插入附件信息
        if(attrs != null && !attrs.isEmpty()) {
            for (ArtistExtAdd.info attr : attrs){
                sysArtistExtService.insertSysArtistExt(new SysArtistExt(artistExtAdd.getProjectId(), artistExtAdd.getArtistId(), artistExtAdd.getArtistName(), 1, attr.getName(), attr.getUrl()));
            }
//            attrs.stream().map(attr -> sysArtistExtService.insertSysArtistExt(new SysArtistExt(artistExtAdd.getProjectId(), artistExtAdd.getArtistId(), artistExtAdd.getArtistName(), 1, attr.getName(), attr.getUrl())));
        }
        return success("新增成功");
    }

}
